package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.dao.*;
import io.renren.dto.AutorescuemDTO;
import io.renren.dto.SaveDetailDto;
import io.renren.entity.AutorescuemEntity;
import io.renren.entity.BusinessmEntity;
import io.renren.entity.UcmlContactEntity;
import io.renren.entity.UcmlOrganizeEntity;
import io.renren.service.AutorescuemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-28
 */
@Service
public class AutorescuemServiceImpl extends CrudServiceImpl<AutorescuemDao, AutorescuemEntity, AutorescuemDTO> implements AutorescuemService {

    @Autowired
    private UcmlOrganizeDao ucmlOrganizeDao;
    @Autowired
    private UcmlContactDao ucmlContactDao;
    @Autowired
    private BusinessmDao businessmDao;

    @Override
    public QueryWrapper<AutorescuemEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AutorescuemEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Autowired
    private AutorescueitemsdDao autorescueitemsdDao;

    @Override
    public Map<String, Object> saveDetail(String traderMOID, SaveDetailDto dto) {
        Map<String, Object> result = new HashMap<>();
        AutorescuemEntity info = baseDao.selectOne(new QueryWrapper<AutorescuemEntity>()
                .eq("AutoRescueMOID", dto.getAutoRescueMoId()));
        if (info != null) {
            result.put("serviceDate", DateUtils.format(info.getBilldate(), "yyyy-MM-dd HH:mm:ss"));
            UcmlOrganizeEntity organizeEntity = ucmlOrganizeDao.selectOne(new QueryWrapper<UcmlOrganizeEntity>()
                    .select("OrgName").eq("UCML_OrganizeOID", info.getOrgFk()));
            result.put("orgName", organizeEntity != null ? organizeEntity.getOrgname() : "");

            UcmlContactEntity ucmlContactEntity = ucmlContactDao.selectOne(new QueryWrapper<UcmlContactEntity>()
                    .select("PersonName").eq("UCML_CONTACTOID", info.getUcmlContactFk()));
            result.put("PersonName", ucmlContactEntity != null ? ucmlContactEntity.getPersonname() : "");

            result.put("BusinessAmount", new BigDecimal(info.getBusinessamount()).setScale(2, BigDecimal.ROUND_HALF_UP));
            result.put("vipDiscount", new BigDecimal(info.getDiscountamount()).setScale(2, BigDecimal.ROUND_HALF_UP));
            result.put("couponDiscount", 0);
            result.put("Amount", new BigDecimal(info.getTotalamount()).setScale(2, BigDecimal.ROUND_HALF_UP));

            BusinessmEntity businessmEntity = businessmDao.selectOne(new QueryWrapper<BusinessmEntity>()
                    .eq("SourceBillOID", info.getAutorescuemoid()));
            result.put("billOID", businessmEntity != null ? businessmEntity.getSettlementmFk() : "");
            List<Map<String, Object>> projectList = autorescueitemsdDao.getProjectList(dto.getAutoRescueMoId());
            result.put("projectList", projectList);
        }


        return result;
    }
}